import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Solution1971UnionFind {
    private int[] root;

    public boolean validPath(int n, int[][] edges, int source, int destination) {
        root = new int[n];
        for (int i = 0; i < n; i++) root[i] = i;
        for (int[] edge : edges) {
            union(edge[0], edge[1]);
            if (find(source) == find(destination)) return true;
        }
        return find(source) == find(destination);
    }

    private void union(int p, int q) {
        root[find(p)] = find(q);
    }

    private int find(int n) {
        return root[n] == n ? n : (root[n] = find(root[n]));
    }


    public static void main(String[] args) {
        System.out.println(new Solution1971UnionFind().validPath(3, new int[][]{{0, 1}, {1, 2}}, 0, 2));
    }

}
